package org.huqiwen.blog.logic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.huqiwen.blog.dao.DataSource;
import org.huqiwen.blog.dao.ExecuteOptionSQL;
import org.huqiwen.blog.data.BlogDataBean;
/**
 * 文章的处理逻辑类，包括文章的分页，增删改查等逻辑操作都由此类完成
 * @author Administrator
 *
 */
public class BlogLogicBean {
	private int allRows;	//总记录数
	private int curPage=1;	//当前页
	private int pageSize=5; //每页显示的数据条数
	private int allPage;	//总页数
	
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getAllRows() {
		return allRows;
	}
	public int getAllPage() {
		return allPage;
	}
	/**
	 * 分页返回查询到的文章结果集，以list返回
	 * @return
	 */
	public List findAllBlog() {
		List list = new ArrayList();
		Statement stmt = null;
		ResultSet rs = null; 
		
		try {
			String sql = "select id,post_title,post_content,post_date,cat_name,post_status,comment_status,post_modified,post_count "
						+"from posts,category "
						+"where posts.post_category = category.cat_id order by id DESC";
			stmt = DataSource.getConnection().createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			rs.last();	//移动到最后一条记录；
			this.allRows = rs.getRow();//得到记录的总条数
			if (this.allRows%this.pageSize == 0) {
				this.allPage = this.allRows/this.pageSize;
			}else {
				this.allPage = this.allRows/this.pageSize + 1;
			}
			
			if (this.curPage ==1) {
				rs.beforeFirst();	//如果是第一页，将指针移动到记录的第一列
			}else {
				rs.absolute((this.curPage-1)*this.pageSize);//否则移动到当前页的第一条记录
			}
			
			int i = 0;
			while(rs.next()&&i<this.pageSize){	//将从数据中查询到数据存储到list中
				BlogDataBean bean = new BlogDataBean();
				bean.setBlogId(rs.getString(1));
				bean.setBlogTitle(rs.getString(2));
				bean.setBlogContent(rs.getString(3));
				bean.setPostData(rs.getString(4));
				bean.setBlogCategory(rs.getString(5));
				bean.setBlogStatus(rs.getString(6));
				bean.setCommentSatatus(rs.getString(7));
				bean.setBlogModdified(rs.getString(8));
				bean.setBlogCount(rs.getString(9));
				list.add(bean);
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if (null!=stmt) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (null!=rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	/**
	 * 此方法可根据传入的SQL语句执行插入、更新、删除操作
	 * @param sql
	 * @return
	 */
	public BlogDataBean findBlogById(String id){
		BlogDataBean bean = new BlogDataBean();
		
		Statement stmt = null;
		ResultSet rs = null; 			
		try {
			String sql1 = "select id,post_title,post_content,post_date,cat_name,post_status,comment_status,post_modified,post_count "
						+"from posts,category "
						+"where posts.post_category = category.cat_id and posts.id="+id;
			String sql2 = "update posts set post_count=post_count+1 where id = "+id;
			stmt = DataSource.getConnection().createStatement();
			stmt.executeUpdate(sql2);
			rs = stmt.executeQuery(sql1);			
			while(rs.next()){
				
				bean.setBlogId(rs.getString(1));
				bean.setBlogTitle(rs.getString(2));
				bean.setBlogContent(rs.getString(3));
				bean.setPostData(rs.getString(4));
				bean.setBlogCategory(rs.getString(5));
				bean.setBlogStatus(rs.getString(6));
				bean.setCommentSatatus(rs.getString(7));
				bean.setBlogModdified(rs.getString(8));
				bean.setBlogCount(rs.getString(9));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if (null!=stmt) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (null!=rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return bean;
	}

	/**
	 * 此方法处理文章的添加操作
	 * @param blogTitle
	 * @param blogContent
	 * @param category
	 * @return
	 */
	public int addBlog(String blogTitle,String blogContent,String category){
		return ExecuteOptionSQL.UpdateSQL("insert into posts(post_title,post_content,post_category,post_date,post_modified,post_count)values('"
				+blogTitle+"','"+blogContent+"','"+category+"',now(),now(),0)");
	}
	/**
	 * 此方法处理文章的编辑后保存的操作
	 * @param blogId
	 * @param blogTitle
	 * @param blogContent
	 * @param category
	 * @return
	 */
	public int editorBlog(String blogId,String blogTitle,String blogContent,String category) {
		return ExecuteOptionSQL.UpdateSQL("update posts set post_title ='"+blogTitle+"',post_content='"+blogContent
				+"',post_category="+category+",post_modified=now() where id="+blogId);
	}
	/**
	 * 此类实现文章的删除操作
	 * @param id
	 * @return
	 */
	public int delBlog(String id) {
		return ExecuteOptionSQL.UpdateSQL("delete from posts where id="+id);
	}
	/**
	 * 此方法没有实现用处，只是用来测试
	 * @param args
	 */
//	public static void main(String[] args) {
//		BlogLogicBean bean = new BlogLogicBean();		
//		System.out.println(bean.findAllBlog().size()+bean.getAllPage());
//	}
}
